<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" @click="handleAdd()" type="primary" icon="el-icon-plus">添加</el-button>
      <el-input class="filter-item" @keyup.enter.native="handleFilter" style="width: 200px;" placeholder="请输入关键词" v-model="search.keyword"></el-input>
      <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table v-loading="!pload" @selection-change="selectionChange" ref="multipleTable" border :data="tableData">
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column property="channelId" label="Id" align="center" show-overflow-tooltip width="100">
      </el-table-column>
      <el-table-column property="channelName" label="名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="templetName" label="别名" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="comment" label="备注" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="添加日期" align="center" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="filter-bottom">
      <el-button-group class="filter-item">
        <el-button @click="toggleSelection(-1)">全选</el-button>
        <el-button @click="toggleSelection(0)" :disabled="!selection.length">反向选择</el-button>
        <el-button @click="toggleSelection(1)" :disabled="!selection.length">取消选择</el-button>
      </el-button-group>
      <el-button-group class="filter-item">
        <el-button @click="handleCreateHtml" :disabled="!selection.length">生成频道静态</el-button>
        <el-button @click="toggleDel" :disabled="!selection.length">删除选中数据</el-button>
      </el-button-group>
    </div>
    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="limit" :page-sizes="limits" background layout="sizes, prev, pager, next, jumper, total" :total="count">
      </el-pagination>
    </div>
    <edit v-if="edit" :data="selectTable" @getData="getData"></edit>
  </div>
</template>

<script>
import * as api from '@/utils/api'
import waves from '@/directive/waves' // 水波纹指令
import edit from './edit'
export default {
  directives: {
    waves
  },
  data() {
    return {
      tableData: [],
      search: {
        keyword: ''
      },
      selection: [],
      selectTable: {},
      pload: false,
      edit: false,
      currentPage: 1,
      limit: 20,
      limits: [10, 20, 50, 100],
      count: 0
    }
  },
  components: {
    edit
  },
  created() {
    this.getData()
  },
  methods: {
    async handleCreateHtml(index, row) {
      await this.$confirm('确认要生成静态吗', { type: 'warning' })
      const ids = this.selection.map((i, index) => {
        return i.channelId
      }).join(',')
      await api.createHtmlByChannel({ channelIds: ids })
      this.$notify({ title: '操作成功，请稍后查看', type: 'success', duration: 3000 })
    },
    selectionChange(selection) {
      this.selection = selection
    },
    toggleSelection(s) {
      if (s === -1) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row, true)
        })
      }
      if (s === 0) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      }
      if (s === 1) {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleAdd() {
      this.selectTable = {}
      this.selectTable.method = 'add'
      this.edit = true
    },
    async handleDelete(index, row) {
      this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
        try {
          this.pload = false
          await api.channelRemove({ channelIds: row.channelId })
          this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
          this.getData()
        } catch (e) {
          this.pload = true
        }
      })
    },
    toggleDel() {
      this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
        const ids = this.selection.map((i, index) => {
          return i.channelId
        }).join(',')
        try {
          this.pload = false
          await api.channelRemove({ channelIds: ids })
          this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
          this.getData()
        } catch (e) {
          this.pload = true
        }
      })
    },
    handleEdit(index, row) {
      this.selectTable = JSON.parse(JSON.stringify(row))
      this.selectTable.method = 'edit'
      this.edit = true
    },
    async getData() {
      const data = this.JSONo(
        {
          pageNum: this.currentPage,
          pageSize: this.limit
        },
        this.search
      )
      this.pload = false
      const Datas = await api.channelList(data)
      this.tableData = Datas.data
      this.count = Datas.pageInfo.total
      this.pload = true
    },
    handleFilter() {
      this.currentPage = 0
      this.getData()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    }
  }
}
</script>
